﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Common;
using RuleCode.Table;

namespace map.member
{
    public partial class pfriends : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof (AjaxFunction  ));
            cbuser.dealcookie();
            if (!IsPostBack)
            {
                BindFriendsCateDropDownList();
                BindFriendsWebPager();
            }
            
        }



        #region 辅助方法
        private void BindFriendsCateDropDownList()
        {
            cbusergroup usergroup = cbusergroup.Find("cbuserid = '" + AliApi.appUserId + "'");             
            this.FriendsCateDropDownList.Items.Clear();
            ListItem listItem = new ListItem("全部好友分类", "");
            this.FriendsCateDropDownList.Items.Add(listItem);
            if (usergroup.Count > 0)
            {
                for (int i = 0; i < usergroup.Count; i++)
                {
                    listItem = new ListItem(usergroup.cbname .Value ,usergroup.cbusergroupid.Value);
                    this.FriendsCateDropDownList.Items.Add(listItem);
                    usergroup.Next();
                }
            }
        }

        private void BindFriendsWebPager()
        {   
            string SqlStr = "exec [GetFriends] @userid='" + AliApi.appUserId + "'";
            if (this.FriendsCateDropDownList.SelectedValue != "")
                SqlStr += ", @usergroupid='"+this.FriendsCateDropDownList.SelectedValue +"'";
            DataSet dSet = SqlHelper.Instance.ExecuteQuery(SqlStr);

            if (dSet.Tables.Count > 0)
            {                   
                this.FriendsWebPager.DataSource = dSet.Tables[0];
                this.FriendsWebPager.DataBind();
                               
                this.NoFriendsDataDiv.Style["display"] =(dSet.Tables[0].Rows.Count == 0) ? "block":"none";
            }
        }
        #endregion

        #region 事件
        protected void FriendsWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            FriendsWebPager.CurrentPageIndex = e.NewPageIndex;
            BindFriendsWebPager();
        }
       
        protected void SelectOthersCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                foreach(DataListItem item in this.FriendsDataList .Items)
                {
                    CheckBox checkBox = item.FindControl("CheckBox0") as CheckBox ;
                    checkBox.Checked = !checkBox.Checked;
                }
            }
        }

        protected void SelectAllCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                foreach (DataListItem item in this.FriendsDataList.Items)
                {
                    CheckBox checkBox = item.FindControl("CheckBox0") as CheckBox;
                    checkBox.Checked = SelectAllCheckBox.Checked  ;
                }
            }
        }

        protected void DeleteSelectedFriendsButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                cbusergrouplist usergrouplist = cbusergrouplist.Find("1=1");
                int index = 0;
                foreach (DataListItem item in this.FriendsDataList.Items)
                {                       
                    CheckBox checkBox = item.FindControl("CheckBox0") as CheckBox;
                    if (checkBox.Checked)
                    {
                        string grouplist_RECID = FriendsDataList.DataKeys[index].ToString();
                        usergrouplist.Delete("RECID=" + grouplist_RECID);
                    }
                    index++;
                }
                BindFriendsWebPager();
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "updatePage", "<script type=\"text/javascript\">initSystemRecommendFriends();</script>", false);
            }
        }

        protected void FriendsCateDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            { 
                BindFriendsWebPager ();
            }
        }

        /// <summary>
        /// 重新刷新页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UpdateBindButton_Click(object sender, EventArgs e)
        {
            BindFriendsWebPager();
        }
        #endregion


    }
}
